package com.example.broadcastmechanismexercise;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;
import android.widget.ImageView;

public class BatteryReceiver extends BroadcastReceiver {

    ImageView imageView;
    public BatteryReceiver(ImageView imageView) {
        this.imageView = imageView;
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                status == BatteryManager.BATTERY_STATUS_FULL;
        if (isCharging) {
            // 充电状态
            imageView.setImageResource(R.drawable.charging_image);
            Log.e("测试", "电池状态是否充电: " + isCharging);
        } else {
            // 非充电状态
            imageView.setImageResource(R.drawable.non_charging_image);
            Log.e("测试", "电池状态是否充电: " + isCharging);
        }
    }
}
